#include <algorithm>
#include <array>
#include <cstring>
#include <iostream>
#include <istream>
#include <iterator>
#include <ostream>
#include <set>
#include <utility>

using ll = long long;
using ull = unsigned long long;
const ll max_num = 6;
short n, arr[max_num];
std::set<std::array<short, max_num>> ans_set;

template<class T,size_t S>
std::ostream &operator<<(std::ostream &os, const std::array<T,S> &s){
    os<<"array [ ";
    if(!s.empty()){
        auto now_it = s.begin();
        const auto back_1_it = s.end()-1;
        while(now_it!=back_1_it){
            os<<*now_it<<", ";
            ++now_it;
        }
        os<<*back_1_it<<" ]";
    }else{
        os<<"}";
    }
    return os;
}

template<class T>
std::ostream &operator<<(std::ostream &os, const std::set<T> &s){
    os<<"set { ";
    if(!s.empty()){
        auto now_it = s.begin();
        auto back_1_it = s.end();
        --back_1_it;
        while(now_it!=back_1_it){
            os<<*now_it<<", ";
            ++now_it;
        }
        os<<*back_1_it<<" }";
    }else{
        os<<"}";
    }
    return os;
}

#ifdef ONLINE_JUDGE
#define DEBUG(code)
#else
#define DEBUG(code)code
#endif

ll arr_to_ll(const ll *const arr){
    ll ret{};
    for(ll i{1};i<=5;i++){
        DEBUG(std::cout<<ret<<'\n';)
        ret=ret*10 + (arr[i]==0?-1:arr[i]);
    }
    DEBUG(std::cout<<ret<<'\n';)
    return ret;
}

int main(){
    std::iostream::sync_with_stdio(false), std::cin.tie(nullptr), std::cout.tie(nullptr);
    std::cin>>n;
    for(ll i{1};i<=n;i++){
        for(ll j{1};j<=5;j++){
            std::cin>>arr[j];
        }
        std::set<std::array<short, max_num>> tmp_set;
        for(ll j{1};j<=5;j++){
            for(ll k{1};k<=9;k++){
                std::array<short, max_num> new_arr{};
                memcpy(new_arr.begin(), arr, sizeof(arr));
                new_arr[j]  = (new_arr[j] + k) % 10;
                DEBUG(
                    for(ll i{1};i<=5;i++){
                        std::cout<<new_arr[i]<<' ';
                    }
                    std::cout<<'\n';
                )
                tmp_set.insert(new_arr);
            }
        }
        for(ll j{1};j<=4;j++){
            for(ll k{1};k<=9;k++){
                std::array<short, max_num> new_arr{};
                memcpy(new_arr.begin(), arr, sizeof(arr));
                new_arr[j]  = (new_arr[j] + k) % 10;
                new_arr[j+1]  = (new_arr[j+1] + k) % 10;
                DEBUG(
                    for(ll i{1};i<=5;i++){
                        std::cout<<new_arr[i]<<' ';
                    }
                    std::cout<<'\n';
                )
                tmp_set.insert(new_arr);
            }
        }
        DEBUG(std::cout<<tmp_set<<'\n';)
        std::set<std::array<short, max_num>> new_ans_set;
        if(i!=1){
            std::set_intersection(ans_set.begin(), ans_set.end(), tmp_set.begin(), tmp_set.end(), std::inserter(new_ans_set, new_ans_set.end()));
        }else{
            new_ans_set = std::move(tmp_set);
        }
        ans_set = std::move(new_ans_set);
        DEBUG(std::cout<<ans_set<<'\n';)
    }
    std::cout<<ans_set.size()<<'\n';
}